﻿using System;
using Sinacor.OMM.Util;

namespace Sinacor.OMM.Deployment.Setup.Steps
{
    public class InstallMSMQ
    {
        #region [ Public Methods ]

        public void Execute(string queueName, Parameter.Enums.SetupMode setupMode)
        {
            if (setupMode == Parameter.Enums.SetupMode.Install)
                this.InstallQueues(queueName);
            else
                this.UninstallQueues(queueName);
        }

        public string GetQueueName(Parameter.Enums.Components component)
        {
            System.Configuration.AppSettingsReader appReader = new System.Configuration.AppSettingsReader();

            string privateQueueRoot = @".\Private$\";

            if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Receptor)
                return string.Concat(privateQueueRoot, appReader.GetValue("ReceptorQueueName", typeof(System.String)).ToString());
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Router)
                return string.Concat(privateQueueRoot, appReader.GetValue("RoteadorQueueName", typeof(System.String)).ToString());
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.Dumper)
                return string.Concat(privateQueueRoot, appReader.GetValue("DumperQueueName", typeof(System.String)).ToString());
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HFTProcess)
                return string.Concat(privateQueueRoot, appReader.GetValue("HFTQueueName", typeof(System.String)).ToString());
            else if (component == Sinacor.OMM.Deployment.Setup.Parameter.Enums.Components.HealthMonitor)
                return string.Concat(privateQueueRoot, appReader.GetValue("HealthMonitorQueueName", typeof(System.String)).ToString());

            return "";
        }

        #endregion

        #region [ Private Methods ]

        private void InstallQueues(string queueName)
        {            
            try
            {
                if (MSMQ.CheckInstalled())
                {
                    if (MSMQ.CheckAccess())
                    {                        
                        MSMQ.Create(queueName);
                    }
                    else
                    {
                        //Não possui acesso
                        throw new Exception();
                    }
                }
                else
                {
                    //Instalar Queue
                    throw new Exception();                    
                }                                
            }
            catch
            {                
                //Não foi possivel instalar
                throw new Exception();
            }            
        }

        private void UninstallQueues(string queueName)
        {
            try
            {                
                MSMQ.Delete(queueName);
            }
            catch
            {
                //Não foi possivel desinstalar
                throw new Exception();
            }
        }

        #endregion
    }
}